package com.graduation.rescuematerial.controller.applet;

import com.graduation.rescuematerial.service.IHelpNewsService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/24 14:48
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/applet/news")
@Api(tags = "Applet——求助新闻")
public class AppletHelpNewsController {

    @Autowired
    private IHelpNewsService helpNewsService;
    @Autowired
    private UserService userService;

    @ApiOperation("发布求助信息")
    @PostMapping("/publish")
    @ResponseBody
    public RespBean publishHelpInfo(@RequestBody HelpNewsVo helpNewsVo, HttpServletRequest request, HttpServletResponse response){
        //获取cookie
//        String ticket= CookieUtil.getCookieValue(request,"userTicket");
        String ticket=helpNewsVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                Long phone=userVo.getUserPhone();
                String companyName=userVo.getCompanyName();
                return helpNewsService.publishHelpInfo(helpNewsVo,phone,companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("组织发布的求助信息查看")
    @PostMapping("/getByCompanyName")
    @ResponseBody
    public RespBean getByCompanyName(@RequestBody String ticket ,HttpServletRequest request, HttpServletResponse response){
        //获取cookie
//        String ticket= CookieUtil.getCookieValue(request,"userTicket");
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                String companyName=userVo.getCompanyName();
                return helpNewsService.getByCompanyName(companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改求助信息状态为已解决")
    @PostMapping("/update/status")
    @ResponseBody
    public RespBean updateStatus(@RequestBody UpdateHelpNewsVo updateHelpNewsVo, HttpServletRequest request,HttpServletResponse response){
        //获取cookie
//        String ticket= CookieUtil.getCookieValue(request,"userTicket");
        String ticket=updateHelpNewsVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getCompanyName()!=null){
                //只能修改本部门下的新闻状态（前面列表只展示了本部门下的列表）
                return helpNewsService.updateStatus(updateHelpNewsVo);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
